package com.jbarreto.vc.dataobjects;

// Generated 13/01/2011 05:26:36 PM by Hibernate Tools 3.2.4.GA

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.jbarreto.vc.hibernate.HibernateUtil;

/**
 * Home object for domain model class Pelicula.
 * @see com.jbarreto.vc.dataobjects.Pelicula
 * @author Hibernate Tools
 */
public class PeliculaHome extends HibernateUtil {

	private static final Log log = LogFactory.getLog(PeliculaHome.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	public void deleteById(int id, Session session) {
		log.debug("deleting Pelicula instance");
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			String hql = "delete from Pelicula pelicula where id = :id";
			Query query = session.createQuery(hql);
			query.setInteger("id", id);
			int row = query.executeUpdate();
			if (row == 0) {
				log.debug("PeliculaHome - Doesn't deleted any row!");
			} else {
				log.debug("PeliculaHome - Deleted Row: " + row);
			}
			tx.commit();
			log.debug("PeliculaHome - delete successful");
		} catch (RuntimeException re) {
			log.error("PeliculaHome - delete failed", re);
			if (tx != null) {
				tx.rollback();
			}
			throw re;
		} finally {
		}
	}


	
	public void attachDirty(Pelicula instance) {
		log.debug("attaching dirty Pelicula instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Pelicula instance) {
		log.debug("attaching clean Pelicula instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Pelicula persistentInstance) {
		log.debug("deleting Pelicula instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Pelicula merge(Pelicula detachedInstance) {
		log.debug("merging Pelicula instance");
		try {
			Pelicula result = (Pelicula) HibernateUtil.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Pelicula findById(java.lang.Integer id) {
		log.debug("getting Pelicula instance with id: " + id);
		try {
			Pelicula instance = (Pelicula) sessionFactory.getCurrentSession()
					.get("com.jbarreto.vc.dataobjects.Pelicula", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Pelicula> findByExample(Pelicula instance) {
		log.debug("finding Pelicula instance by example");
		try {
			List<Pelicula> results = (List<Pelicula>) sessionFactory
					.getCurrentSession().createCriteria(
							"com.jbarreto.vc.dataobjects.Pelicula").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
